<div id="tb<?php echo $uniqid;?>">
    <table cellspacing="0" cellpadding="0">
        <tbody>
        <tr>
            <td><a href="javascript:void(0);" onclick="goodsPackageAdd();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-tianjia1"></i><span class="l-btn-text">增加</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:void(0);" onclick="goodsPackageEdit();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-bianji"></i><span class="l-btn-text">编辑</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:void(0);" onclick="goodsPackageRecycle();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shanchu"></i><span class="l-btn-text">回收站</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><a href="javascript:void(0);" onclick="goodsPackageReload();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-shuaxin"></i><span class="l-btn-text">重载</span></span></a></td>
            <td>
                <div class="datagrid-btn-separator"></div>
            </td>
            <td><span style="line-height:30px;"></span>
                <input id="goodsPackagekeywords" style="width:200px" placeholder="按ID、商品名搜索" type="text"
                       class="easyui-input textbox"></td>
            <td><a href="javascript:void(0);" onclick="goodsPackageSearch();" class="l-btn l-btn-small l-btn-plain"><span
                    class="l-btn-left l-btn-icon-left"><i class="iconfont icon-sousuo"></i><span class="l-btn-text">查询</span></span></a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="con"  style="-moz-user-select:none;">
    <table id="goodsPackageManage"></table>
</div>
<div id="goodsPackagedlg" class="easyui-dialog" style="width:720px; height:460px;" title="信息框" closed="true"></div>
<script>
    var goodsPackageEdit = function (id) {
        let title = '编辑商品套餐';
        let rowId = id || ($('#goodsPackageManage').datagrid('getSelected') && $('#goodsPackageManage').datagrid('getSelected').id);
        if (rowId) {
            let url = '/admin/goodspackage/edit?id=' + rowId;
            $('#rightTabs').tabs('close', title);
            addTab(title, url);
        } else {
            $.messager.alert('提示', '请先选择要编辑的记录！', 'warning');
        }
    };
    var goodsPackageAdd = function () {
        let title = '新增商品套餐';
        $('#rightTabs').tabs('close', title);
        addTab(title, '/admin/goodspackage/add');
    };
    var goodsPackageUp = function (id) {
        let rowId = id || ($('#goodsPackageManage').datagrid('getSelected') && $('#goodsPackageManage').datagrid('getSelected').id);
        if (rowId) {
            $.post('/admin/goodspackage/up', {id: rowId}, function (data) {
                $.messager.progress('close');
                if (parseInt(data.ret) === 0) {
                    $.messager.show({
                        title: '提示',
                        msg: data.msg,
                        timeout: 3000,
                        showType: 'slide'
                    });
                    goodsPackageReload();
                } else {
                    $.messager.alert('提示', data.msg, 'warning');
                }
            }, 'json');
        }
    };
    var goodsPackageDown = function (id) {
        let rowId = id || ($('#goodsPackageManage').datagrid('getSelected') && $('#goodsPackageManage').datagrid('getSelected').id);
        if (rowId) {
            $.post('/admin/goodspackage/down', {id: rowId}, function (data) {
                $.messager.progress('close');
                if (parseInt(data.ret) === 0) {
                    $.messager.show({
                        title: '提示',
                        msg: data.msg,
                        timeout: 3000,
                        showType: 'slide'
                    });
                    goodsPackageReload();
                } else {
                    $.messager.alert('提示', data.msg, 'warning');
                }
            }, 'json');
        }
    };
    var goodsPackageRecycle = function (id) {
        let rowId = id || ($('#goodsPackageManage').datagrid('getSelected') && $('#goodsPackageManage').datagrid('getSelected').id);
        if (rowId) {
            $.messager.confirm('提示', '确定放回收站吗？', function (r) {
                if (r) {
                    $.messager.progress();
                    $.post('/admin/goodspackage/recycle', {id: rowId}, function (data) {
                        $.messager.progress('close');
                        if (parseInt(data.ret) === 0) {
                            $.messager.show({
                                title: '提示',
                                msg: data.msg,
                                timeout: 3000,
                                showType: 'slide'
                            });
                            goodsPackageReload();
                        } else {
                            $.messager.alert('提示', data.msg, 'warning');
                        }
                    }, 'json');
                }
            });
        } else {
            $.messager.alert('提示', '请先选择要扔回收站的行！', 'warning');
        }
    };
    var goodsPackageReload = function () {
        $('#goodsPackageManage').datagrid('reload');
    };
    var goodsPackageSearch = function (id) {
        var height = $(window).height() - $(".top").height() - 46;//表格高度
        var width = $(window).width() - $(".leftmenu").width() - 8;
        $('#goodsPackageManage').datagrid({
            title: '商品套餐管理',
            width: width,
            height: height,//高度
            singleSelect: true,//如果为true，则只允许选择一行
            striped: true,//是否显示斑马线效果。
            rownumbers: false,//如果为true，则显示一个行号列。
            pagination: true,//是否显示分页
            pageSize: 15,
            pageList: [15, 30, 40, 50],
            method: 'post',
            sortName: 'weigh',
            sortOrder: 'desc',
            url: '/admin/goodspackage/index',
            queryParams: {
                keywords: $('#goodsPackagekeywords').val(),
                category_ids: id,
                label_id: $('#goodsPackagelabel_id').val(),
                up: $("input[name='goodsPackageup']").prop('checked'),
                down: $("input[name='goodsPackagedown']").prop('checked'),
                is_top: $("input[name='goodsPackageis_top']").prop('checked'),
                is_hot: $("input[name='goodsPackageis_hot']").prop('checked'),
                is_new: $("input[name='goodsPackageis_new']").prop('checked'),
                stock: $('#goodsPackagestock').val()
            },
            fitColumns: true,//真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            nowrap: false,//如果为true，则在同一行中显示数据。设置为true可以提高加载性能。 是否换行
            selectOnCheck: false,
            checkOnSelect: true,
            toolbar: '#tb<?php echo $uniqid;?>',
            rowStyler: function (index, row) {
                if (row.status == '失效') {
                    return 'color:#F00;font-style:italic;';
                }
            },
            columns: [[
                {field: 'id', title: 'ID', width: fixWidth(3),sortable: true},
                {
                    field: 'title', title: '名称', width: fixWidth(35), formatter: function (value, rowData, rowIndex) {
                        return '<div style="display: flex;align-items: center;">' +
                            '<img src="' + rowData.image + '?imageView2/1/w/60/h/60" style="height:60px;width:60px;border-radius: 5px;border:#dfdfdf 1px solid;">'+
                            '<div>' + value + "<br>" + (rowData.is_sku ? '（多规格）' : '') + '</div></div>';
                    }
                },
                {field: 'price', title: '价格', width: fixWidth(5), align:'center', sortable: true},
                {field: 'sales', title: '销量', width: fixWidth(5), align:'center', sortable: true},
                {field: 'views', title: '浏览量', width: fixWidth(5), align:'center', sortable: true},
                {field: 'stock', title: '库存', width: fixWidth(5), align:'center', sortable: true},
                {field: 'updated_at', title: '更新时间', width: fixWidth(10), align:'center', sortable: true},
                {field: 'weigh', title: '排序', width: fixWidth(5), align:'center', sortable: true},
                {
                    field: 'status', title: '操作', align:'center', width: fixWidth(15), formatter: function (value, rowData, rowIndex) {
                        let buttons = '<div class="table"><span class="l-btn l-btn-small color9 table" onclick="goodsPackageEdit(\'' + rowData.id + '\')"><span class="l-btn-text">编辑</span></span>';
                        if (rowData.status==='down') {
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="goodsPackageUp(\'' + rowData.id + '\')"><span class="l-btn-text">立即上架</span></span>';
                        }
                        if (rowData.status==='up') {
                            buttons += '<span class="l-btn l-btn-small color9 table" onclick="goodsPackageDown(\'' + rowData.id + '\')"><span class="l-btn-text" style="color: red">立即下架</span></span>';
                        }
                        buttons += '<span class="l-btn l-btn-small color9 table" onclick="goodsPackageRecycle(\'' + rowData.id + '\')"><span class="l-btn-text">删除</span></span>';
                        return buttons + '</div>';
                    }
                },
            ]],
            onDblClickRow: function (index, field) {
                goodsPackageEdit();
            }
        });
    };
    goodsPackageSearch();
    onDatagridResize("#goodsPackageManage");
</script>
